<?php
// @formatter:off
/**
 * @file s.directories.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-07-05 14:30:06 +0000 (Fri, 05 Jul 2013) $
 *
 * $Id: s.directories.php 89 2013-07-05 14:30:06Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0/services/s.directories.php $
 */
// @formatter:on

$wcServiceStatus = false;
$wcServiceErrorCode = 500;

if(defined('__SERVICE__') && $wcGroup && !$wcGroupForbidden && $wcProfile->allowedAny(WC_PERM_COMIC_VIEW, WC_PERM_DIRECTORY_VIEW, WC_PERM_DIRECTORY_LIST, WC_PERM_DIRECTORY_RENAME)) {
	/*
	 * Conditions:
	 *
	 * Request Method GET (not POST):
	 *	- When there's a comic id specified ($wcComic) and the requesting
	 *	  user is allowed to view comics, the result is the comic's
	 *	  directory id, base-name and path.
	 *	- When there's a directory id specified ($wcDirectory) and the
	 *	  requesting user is allowed to list directories, the result is
	 *	  the list of sub-directories belonging to the specified one.
	 *	  When the specified directory is a leaf, tag "wc-result" is
	 *	  returned empty.
	 *	- When there's no comic or directory set, but there's a parameter
	 *	  called "pattern" set and the requesting user is allowed to view
	 *	  and list directories, the service returns the list of
	 *	  directories that match with the specified pattern.
	 *	- In other case, it returns an parameters error message.
	 *
	 * Request Method POST:
	 *	- When there's the parameter "rename" ($_REQUEST), there's a
	 *	  directory specified ($wcDirectory), the requesting user is
	 *	  allowed to rename directories and a parameter called NEXT_NAME
	 *	  is set in post data, the service will attempt to schedule a
	 *	  renaming operation for the directory with the new name.
	 *	- In other case, it returns an parameters error message.
	 */
	if($_SERVER["REQUEST_METHOD"] != "POST") {
		if($wcComic && $wcProfile->allowedTo(WC_PERM_COMIC_VIEW)) {
			//
			// Comic tags list requested.
			$directory = $wcComic->directory();
			if($directory) {
				$wcServiceStatus = true;
				$wcServiceXMLResult .= "<directory>";
				$wcServiceXMLResult .= "<id>{$directory->id}</id>";
				$wcServiceXMLResult .= "<path>{$directory->name}</path>";
				$wcServiceXMLResult .= "<basename>{$directory->basename()}</basename>";
				$wcServiceXMLResult .= "</directory>";
			}
		} elseif($wcDirectory && $wcProfile->allowedTo(WC_PERM_DIRECTORY_LIST)) {
			//
			// Directory's directories list requested.
			$wcServiceStatus = true;

			$items = $wcDirectory->subDirectories();
			uasort($items, "WCDirectory::NameSort");
			foreach($items as $directory) {
				$wcServiceXMLResult .= "<directory>";
				$wcServiceXMLResult .= "<id>{$directory->id}</id>";
				$wcServiceXMLResult .= "<path>{$directory->name}</path>";
				$wcServiceXMLResult .= "<basename>{$directory->basename()}</basename>";
				$wcServiceXMLResult .= "</directory>";
			}
		} elseif(isset($_REQUEST["pattern"]) && $wcProfile->allowedTo(WC_PERM_DIRECTORY_LIST, WC_PERM_DIRECTORY_VIEW)) {
			global $wcDirectoriesHolder;
			global $wcPageCache;

			$wcPageCache->anyDirectory();

			$pattern = $_REQUEST["pattern"];
			$limit = isset($_REQUEST["limit"]) ? $_REQUEST["limit"] : 20;

			$wcServiceStatus = true;
			foreach($wcDirectoriesHolder->directoriesLike($pattern, $wcGroup) as $directory) {
				$wcServiceXMLResult .= "<directory>";
				$wcServiceXMLResult .= "<id>{$directory->id}</id>";
				$wcServiceXMLResult .= "<path>{$directory->name}</path>";
				$wcServiceXMLResult .= "<basename>{$directory->basename()}</basename>";
				$wcServiceXMLResult .= "</directory>";

				$limit--;
				if(!$limit) {
					break;
				}
			}
		} else {
			$wcServiceErrorMsg = "This service was call with invalid parameters";
		}
	} elseif($_SERVER["REQUEST_METHOD"] == "POST") {
		if(isset($_REQUEST["rename"]) && isset($_POST["NEXT_NAME"]) && strlen(trim($_POST["NEXT_NAME"])) && $wcProfile->allowedTo(WC_PERM_DIRECTORY_RENAME)) {
			if($wcDirectory && $wcDirectory->ok()) {
				if(count($wcDirectory->subDirectories()) == 0) {
					$next = trim($_POST["NEXT_NAME"]);
					$name = $wcDirectory->name;

					if(!$next) {
						$wcServiceErrorMsg = "No new name specified";
					} else {
						$wcServiceStatus = true;
						$wcDirectory->next_name = $next == $name ? "" : $next;
					}
				} else {
					$wcServiceErrorMsg = "Only directories with no sub-directories can be renamed";
				}
			} else {
				$wcServiceErrorMsg = "This service was call with invalid parameters";
			}
		} else {
			$wcServiceErrorMsg = "This service was call with invalid parameters";
		}
	}
} else {
	$wcServiceErrorCode = 403;
	$wcServiceErrorMsg = "Unable to access service";
}
?>